from PyQt5.QtWidgets import *
from myutil import globalvar as gl, logs
from myutil.api.jwc_data_process import course_list_element_to_dict
from ui.enable_select_course import Ui_Frame as enable_select_course
from PyQt5.QtGui import QStandardItemModel, QStandardItem


class enable_select_course_form(QFrame, enable_select_course):
    def __init__(self, parent=None):
        super(enable_select_course_form, self).__init__(parent)
        self.setupUi(self)

        self.fresh_enable_course_tableView()

        self.enable_kccx_pushButton.clicked.connect(self.fresh_enable_course_tableView)

    def fresh_enable_course_tableView(self):
        logs.logs_print("fresh_可以选的课程")

        if gl.get_value("free_course_list") is None:
            self.label.setText("请先登录，再查看，或者已经过了选课的时间")
            return 0

        self.free_course_list = gl.get_value("free_course_list")

        enable_kccx = str(self.enable_kccx_lineEdit.text())
        logs.logs_print("enable_kccx:" + enable_kccx)

        res = []
        for i in self.free_course_list:
            # res.append({"info": i})
            if enable_kccx == "" or enable_kccx in i:
                res.append(course_list_element_to_dict(i))

        if len(res) == 0:
            logs.logs_print("结果为空")
            model = QStandardItemModel(1, 1)
            model.setHorizontalHeaderLabels(["结果"])
            model.setItem(0, 0, QStandardItem("结果为空"))
            self.enable_course_tableView.setModel(model)
            return

        columns = res[0].keys()
        # 设置数据层次结构，4行4列
        model = QStandardItemModel(len(res), len(columns))
        # 设置水平方向四个头标签文本内容
        model.setHorizontalHeaderLabels(columns)

        for i in range(len(res)):
            for column, data in enumerate(res[i].values()):
                item = QStandardItem(str(data))
                # 设置每个位置的文本值
                model.setItem(i, column, item)

        # 实例化表格视图，设置模型为自定义的模型
        self.enable_course_tableView.setModel(model)
